﻿using GalaSoft.MvvmLight;
using Jn698._45GWTool.Models.DataGrid;
using Jn698._45GWTool.Services;
using Jn698._45Tool.Service;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WebFirst.Entities;
using WindowsFormsApp1.Utils;

namespace Jn698._45GWTool.ViewModels.Controls.DataControls
{
    public class DayRateTotalViewModel:ViewModelBase
    {
		private GWCmdMakeUpService GWCmdMakeUpService = new GWCmdMakeUpService();
		private GWCmdMakeUpPlanService GWCmdMakeUpPlanService = new GWCmdMakeUpPlanService();
		public static GWCmdMakeUp GWCmdMakeUp;
        public static GWCmdMakeUpPlan GWCmdMakeUpPlan;
        private int PreSelectedIndex=-1;

        public string[] DayRates;
		public SingleData singleData;
        private ObservableCollection<RCSDDataModel> dayRateList;

		public ObservableCollection<RCSDDataModel> DayRateList
        {
			get { return dayRateList; }
			set { dayRateList = value; RaisePropertyChanged(); }
		}

		private int selectedIndex;

		public int SelectedIndex
		{
			get { return selectedIndex; }
			set {
				
                
				if (selectedIndex!= value)
				{
					selectedIndex = value;
					RaisePropertyChanged();
                    GetDayRateData(value);
                }
                /*if (DayRates != null)
                {
                    //SaveData();
                    GetDayRateData(value);
                }*/



            }
		}

		private int totalNumber;

		public int TotalNumber
		{
			get { return totalNumber; }
			set {
				int number = totalNumber;

                Set(ref totalNumber, value);
				if (number != value)
				{
                    GetDatRateNumber(value);
                }
				
			}
		}

		private ObservableCollection<DayRateData> dayRateDataList;

		public ObservableCollection<DayRateData> DayRateDataList
		{
			get { return dayRateDataList; }
			set { dayRateDataList = value; RaisePropertyChanged(); }
		}

        public DayRateTotalViewModel()
        {
			DayRateDataList = new ObservableCollection<DayRateData>();
			DayRateList = new ObservableCollection<RCSDDataModel>();
        }
        /// <summary>
        /// 更新GWCmdMakeUp，获取日时段表的个数
        /// </summary>
        public void UpdateData()
		{
			if (MainViewModel.TopIndex==0)
			{
                if (GWCmdMakeUp == null)
                {
                    return;
                }
                if (GWCmdMakeUp.SubItemName == null)
                {
                    return;
                }
                DayRates = GWCmdMakeUp.DataValue?.Split(';');
            }
			else
			{
                if (GWCmdMakeUpPlan == null)
                {
                    return;
                }
                if (GWCmdMakeUpPlan.SubItemName == null)
                {
                    return;
                }
                DayRates = GWCmdMakeUpPlan.DataValue?.Split(';');
            }
            GetDayRateData(0);
            
            TotalNumber = DayRates.Length;
			
        }

		public void SaveData()
		{
			/*if (singleData ==null)
			{
				return;
			}*/
			if (DayRateList.Count<=0|| PreSelectedIndex>= DayRateList.Count)
			{
				return;
			}
			StringBuilder currentDauRateData = new StringBuilder();
			StringBuilder stringBuilder = new StringBuilder();
			for (int i = 0; i < DayRateDataList.Count; i++)
			{
				DayRateData dayRateData = DayRateDataList[i];
                currentDauRateData.Append(dayRateData.Hour);
                currentDauRateData.Append(',');
                currentDauRateData.Append(dayRateData.Min);
                currentDauRateData.Append(',');
                currentDauRateData.Append(dayRateData.Rate);
                currentDauRateData.Append(',');
            }
			DayRateList[PreSelectedIndex].Data=currentDauRateData.ToString();
			SaveGWCmdMakeUp();
            /*if (currentDauRateData.Length<=0)
			{

				return;
			}
			if (DayRates.Length< PreSelectedIndex + 1)
			{
				stringBuilder.Append(GWCmdMakeUp.DataValue);

                for (int i = 0; i < PreSelectedIndex + 1- DayRates.Length; i++)
				{
					stringBuilder.Append(';');
					if (i+ DayRates.Length== PreSelectedIndex)
					{
						stringBuilder.Append(currentDauRateData.ToString());

                    }
                }
			}
			else
			{
				DayRates[PreSelectedIndex] = currentDauRateData.ToString();

            }*/
            //GWCmdMakeUp.DataValue=
            //GWCmdMakeUpService.Update(GWCmdMakeUp);

        }

		public void SaveGWCmdMakeUp()
		{
            StringBuilder stringBuilder = new StringBuilder();
			int point = -1;
            for (int i = DayRateList.Count-1; i >=0; i--)
            {
				if(!string.IsNullOrWhiteSpace(DayRateList[i].Data))
				{
					point = i;
					break;

                }
            }
			for (int i = 0; i <= point; i++)
			{
				stringBuilder.Append(DayRateList[i].Data);

                if (i+1<=point)
				{
                    stringBuilder.Append(';');
                }
				
			}
			if (MainViewModel.TopIndex==0)
			{
                GWCmdMakeUp.DataValue = stringBuilder.ToString();

                GWCmdMakeUpService.Update(GWCmdMakeUp);
            }
			else
			{
                GWCmdMakeUpPlan.DataValue = stringBuilder.ToString();

                GWCmdMakeUpPlanService.Update(GWCmdMakeUpPlan);
            }
        }
        /// <summary>
        /// 显示index序号的日时段表的数据
        /// </summary>
        /// <param name="index"></param>
        public void GetDayRateData(int index)
		{
			if (SelectedIndex<0)
			{
				return;
			}
			if (PreSelectedIndex>=0)
			{
				SaveData();
			}
			string data = "";
			/*if (index< DayRates.Length)
			{
				data = DayRates[index];

            }*/
			if (index<DayRateList.Count)
			{
				data = DayRateList[index].Data;

            }
			var datas = data.Split(',');
			var num = datas.Length / 3;
			DayRateDataList.Clear();
            for (int i = 0; i < num; i++)
			{
                DayRateData dayRateData = new DayRateData();
				dayRateData.Hour = datas[3 * i];
				dayRateData.Min = datas[3 * i + 1];
				dayRateData.Rate = datas[3 * i + 2];
				DayRateDataList.Add(dayRateData);

            }
			/*if (singleData==null)
			{
				singleData = new SingleData();

            }
			singleData.Name = index.ToString();*/
			PreSelectedIndex = SelectedIndex;
        }
		/// <summary>
		/// 获取<paramref name="num"/>的日时段表序号的列表，并默认为第一个日时段表
		/// </summary>
		/// <param name="num"></param>
		public void GetDatRateNumber(int num)
		{
			if (MainViewModel.TopIndex==0)
			{
                DayRates = GWCmdMakeUp.DataValue?.Split(';');
            }
			else
			{
                DayRates = GWCmdMakeUpPlan.DataValue?.Split(';');
            }
            DayRateList.Clear();
            for (int i = 0; i < num; i++)
            {
				RCSDDataModel RCSDDataModel = new RCSDDataModel();
				RCSDDataModel.CSD = (i + 1).ToString();
				
				RCSDDataModel.Data = i < DayRates.Length ? DayRates[i]:"";
                DayRateList.Add(RCSDDataModel);

            }
			GetDayRateData(selectedIndex);
            SelectedIndex = 0;
        }
	}
}
